package com.xing.ui.button.normalButton;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.appcompat.widget.AppCompatButton;

import com.xing.ui.R;
import com.xing.ui.drawable.RoundDrawable;

public class NormalButton extends AppCompatButton {

    protected float radius, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius;

    protected float mBorderWidth;

    protected int mBorderColor;

    private Drawable mDrawable;

    public NormalButton(Context context) {
        this(context, null);
    }

    public NormalButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NormalButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {

        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.NormalButton);

        radius = array.getDimension(R.styleable.NormalButton_radius, 0f);
        topLeftRadius = array.getDimension(R.styleable.NormalButton_topLeftRadius, 0f);
        topRightRadius = array.getDimension(R.styleable.NormalButton_topRightRadius, 0f);
        bottomLeftRadius = array.getDimension(R.styleable.NormalButton_bottomLeftRadius, 0f);
        bottomRightRadius = array.getDimension(R.styleable.NormalButton_bottomRightRadius, 0f);
        mBorderWidth = array.getDimension(R.styleable.NormalButton_border_width, 0f);
        mBorderColor = array.getColor(R.styleable.NormalButton_border_width, 0x00000000);

        if (radius != 0) {
            topLeftRadius = radius;
            topRightRadius = radius;
            bottomLeftRadius = radius;
            bottomRightRadius = radius;
        }

        if (mDrawable != null) {
            updateAttrs(mDrawable);
            super.setBackgroundDrawable(mDrawable);
        }

        array.recycle();

    }

    @Override
    public void setBackground(Drawable background) {
        setBackgroundDrawable(background);
    }

    @Override
    public void setBackgroundColor(int color) {
        setBackground(new ColorDrawable(color));
    }

    @Override
    public void setBackgroundDrawable(Drawable background) {
        mDrawable = RoundDrawable.fromDrawable(background);
        updateAttrs(mDrawable);
        super.setBackgroundDrawable(mDrawable);
    }

    private void updateAttrs(Drawable drawable) {
        if (drawable == null) {
            return;
        }

        if (drawable instanceof RoundDrawable) {
            ((RoundDrawable) drawable)
                    .setScaleType(ImageView.ScaleType.FIT_XY)
                    .setBorderWidth(mBorderWidth)
                    .setBorderColor(mBorderColor);

            ((RoundDrawable) drawable).setCornerRadius(
                    topLeftRadius,
                    topRightRadius,
                    bottomRightRadius,
                    bottomLeftRadius);

        } else if (drawable instanceof LayerDrawable) {
            // loop through layers to and set drawable attrs
            LayerDrawable ld = ((LayerDrawable) drawable);
            for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
                updateAttrs(ld.getDrawable(i));
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public float getTopLeftRadius() {
        return topLeftRadius;
    }

    public void setTopLeftRadius(float topLeftRadius) {
        this.topLeftRadius = topLeftRadius;
    }

    public float getTopRightRadius() {
        return topRightRadius;
    }

    public void setTopRightRadius(float topRightRadius) {
        this.topRightRadius = topRightRadius;
    }

    public float getBottomLeftRadius() {
        return bottomLeftRadius;
    }

    public void setBottomLeftRadius(float bottomLeftRadius) {
        this.bottomLeftRadius = bottomLeftRadius;
    }

    public float getBottomRightRadius() {
        return bottomRightRadius;
    }

    public void setBottomRightRadius(float bottomRightRadius) {
        this.bottomRightRadius = bottomRightRadius;
    }
}
